package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tan.model.domain.BlackList;
import com.tan.model.domain.UserInfo;
import com.tanhua.dubbo.mapper.BlackListMapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class BlackListApiImpl implements BlackListApi{


    @Autowired
    private BlackListMapper blackListMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public IPage<UserInfo> getBlackList(Long userId, int page, int pagesize) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        IPage<UserInfo> ipage = new Page<>(page,pagesize);
        List<UserInfo> userInfos = new ArrayList<>();
      List<BlackList> blackLists =  blackListMapper.selectList(queryWrapper);
      blackLists.forEach(item->{
          Long blackUserId = item.getBlackUserId();
          UserInfo userInfo = userInfoMapper.selectById(blackUserId);
          userInfos.add(userInfo);

      });
         ipage.setRecords(userInfos);
         ipage.setTotal(userInfos.size());
        return ipage;
    }

    @Override
    public void deleteBlackList(Long blackUserId,Long userId ) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("black_user_id", blackUserId);
        queryWrapper.eq("user_id", userId);
        blackListMapper.delete(queryWrapper);

    }
}
